﻿using System;
using System.Data;
using VegetableSale.NET.Entities;
using VegetableSale.NET.Utils;

namespace VegetableSale.NET.Dao;

public class CustomerMapper
{
    public List<Customer> getCustList(QueryDTO queryDTO)
    {
        String sql = "";

        if (queryDTO.Keyword.Equals(""))
        {
            sql = "select * from customer;";
        }
        else
        {
            sql = "SELECT * FROM customer WHERE custname='" + queryDTO.Keyword + "';";
        }

        List<Customer> customers = new List<Customer>();

        DataSet dateSet = MySqlHelper.GetDataSet(sql);
        DataTable dataTable = dateSet.Tables[0];

        foreach (DataRow row in dataTable.Rows)
        {
            uint id = (uint) row[0];
            string custlogin = (string) row[1];
            string custpass = (string) row[2];
            string custname = (string) row[3];
            string phone = (string) row[4];
            string custsex = (string) row[5];
            Customer customer = new Customer(id, custlogin, custpass, custname, phone, custsex);
            customers.Add(customer);
        }

        return customers;
    }

    public void addCustomer(QueryCustomer queryCustomer)
    {
        //MySqlHelper mySqlHelper = new MySqlHelper();

        String sql = "INSERT INTO customer (custlogin,custpass,custname,phone,custsex) values ('" +
                     queryCustomer.Custlogin + "','" + queryCustomer.Custpass + "','" + queryCustomer.Custname + "','" +
                     queryCustomer.Phone + "','" + queryCustomer.Custsex + "');";

        MySqlHelper.ExecuteSql(sql);
    }

    public void custdelete(int custid)
    {
        //MySqlHelper mySqlHelper = new MySqlHelper();

        String sql = "DELETE FROM customer WHERE custid=" + custid + ";";

        MySqlHelper.ExecuteSql(sql);
    }

    public void custupdate(Customer customer)
    {
        String sql = "UPDATE customer SET custlogin='" + customer.Custlogin + "',custname='" + customer.Custname +
                     "',custpass='" + customer.Custpass + "',custsex='" + customer.Custsex + "',phone='" +
                     customer.Phone + "' WHERE custid=" + customer.Custid + ";";

        MySqlHelper.ExecuteSql(sql);
    }

    public List<Customer> getALLCust()
    {
        string sql = "select * from customer;";
        
        List<Customer> customers = new List<Customer>();

        DataSet dateSet = MySqlHelper.GetDataSet(sql);
        DataTable dataTable = dateSet.Tables[0];

        foreach (DataRow row in dataTable.Rows)
        {
            uint id = (uint) row[0];
            string custlogin = (string) row[1];
            string custpass = (string) row[2];
            string custname = (string) row[3];
            string phone = (string) row[4];
            string custsex = (string) row[5];
            Customer customer = new Customer(id, custlogin, custpass, custname, phone, custsex);
            customers.Add(customer);
        }
        return customers;
    }
}